package com.yunfei.api.v2.service;
import com.yunfei.api.v2.param.faQiQianShu.FaQiDianZiHeTongQianShuParam;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.HeTongSignInfoGetStatusResultStatus;
import com.yunfei.enums.ResultStatus;
import com.yunfei.mapper.aiiidb.HeTongSignInfoMapper;
import com.yunfei.mapper.crm.GetProjIdMapper;
import com.yunfei.utils.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service
public class HTSFaQiQianShuService {
    @Autowired
    private HeTongSignInfoMapper heTongSignInfoMapper;
    @Autowired
    private GetProjIdMapper getProjIdMapper;
    /**
    * @Description: 发起电子合同签署流程(理财师)
    * @Param: [param]
    * @return: com.yunfei.bo.result.ResultModel
    * @Author: zhengyunfei
    * @Date: 2019/8/26
    * @time:14:20
    */
    public ResultModel faQiDianZiHeTongQianShuLiuCheng(FaQiDianZiHeTongQianShuParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String reserveNo=param.getcReserveNo();//预约编号
            String creatorId=param.getCreatorId();//创建用户，获取员工id
            if(StringUtil.isNullOrEmpty(reserveNo)){
                resultModel.setCode(HeTongSignInfoGetStatusResultStatus.RESERVENO_NOTNULL.getCode());
                resultModel.setMessage(HeTongSignInfoGetStatusResultStatus.RESERVENO_NOTNULL.getMessage());
                return resultModel;
            }
            if(StringUtil.isNullOrEmpty(creatorId)){
                resultModel.setCode(HeTongSignInfoGetStatusResultStatus.CREATEOR_ID_NOTNULL.getCode());
                resultModel.setMessage(HeTongSignInfoGetStatusResultStatus.CREATEOR_ID_NOTNULL.getMessage());
                return resultModel;
            }
            queryMap.put("reserveNo",reserveNo);
            //判断合同关键信息是否录入完成
            //1.判断合同模板 管理人公章 法人公章 作废协议是否上传
            int isUploadMuBan=heTongSignInfoMapper.isUploadMuBan(queryMap);
            if(isUploadMuBan==0){
                resultModel.setCode(HeTongSignInfoGetStatusResultStatus.INFO1.getCode());
                resultModel.setMessage(HeTongSignInfoGetStatusResultStatus.INFO1.getMessage());
                return resultModel;
            }
            //2.判断合同需要录入的信息是否上传
            int isUploadLuRuInfo=heTongSignInfoMapper.isUploadLuRuInfo(queryMap);
            if(isUploadLuRuInfo==0){
                resultModel.setCode(HeTongSignInfoGetStatusResultStatus.INFO2.getCode());
                resultModel.setMessage(HeTongSignInfoGetStatusResultStatus.INFO2.getMessage());
                return resultModel;
            }
            //3.判断电子合同编号是否充足
            //获取营销项目id
            String projId=getProjIdMapper.get(queryMap);
            Map<String,Object> sequenceNameMap=new HashMap<>();
            sequenceNameMap.put("sequenceName","SEQ_ELEC_CONT_CON_NO_"+projId);
            String isChongZuFlag=heTongSignInfoMapper.isEtNoChongZu(sequenceNameMap);
            if("N".equals(isChongZuFlag)){
                resultModel.setCode(HeTongSignInfoGetStatusResultStatus.INFO3.getCode());
                resultModel.setMessage(HeTongSignInfoGetStatusResultStatus.INFO3.getMessage());
                return resultModel;
            }
          //保存数据
            heTongSignInfoMapper.faQiDianZiHeTongQianShu(param);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
}
